﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="utf-8" version="1.0"/>

<xsl:template match="/">
<root id="{/root/@id}">
<xsl:copy-of select="/root/username"/>
<xsl:copy-of select="/root/password"/>
<xsl:copy-of select="/root/addtime"/>
<xsl:copy-of select="/root/email"/>
<xsl:copy-of select="/root/actionip"/>
<favorites>
<xsl:call-template name="getCatTree">
	<xsl:with-param name="pid" select="0" />
</xsl:call-template>
<xsl:apply-templates select="/root/favorites/fav[@catid='0']"/>
</favorites>
</root>
</xsl:template>

<xsl:template name="getCatTree">
<xsl:param name="pid" />
<xsl:for-each select="/root/category/cat[@pid=$pid]">
	<folder name="{.}" addtime="{@addtime}" default="{@default}">
    	<xsl:if test="/root/category/cat[@pid=current()/@id]">
        	<xsl:call-template name="getCatTree">
            	<xsl:with-param name="pid" select="@id" />
            </xsl:call-template>
        </xsl:if>
        <xsl:for-each select="/root/favorites/fav[@catid=current()/@id]">
    		<xsl:copy-of select="." />
    	</xsl:for-each>
    </folder>    
</xsl:for-each>
</xsl:template>

<xsl:template match="/root/favorites/fav">
<xsl:copy-of select="." />
</xsl:template>

</xsl:stylesheet>